package cn.iocoder.yudao.module.blog.controller.app.space;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.blog.controller.admin.space.vo.BlogSpaceCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.space.vo.BlogSpacePageReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.spacecomment.vo.BlogSpaceCommentCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.spacecomment.vo.BlogSpaceCommentRespVO;
import cn.iocoder.yudao.module.blog.controller.app.space.commentvo.AppBlogSpaceCommentPageReqVO;
import cn.iocoder.yudao.module.blog.controller.app.space.commentvo.AppBlogSpaceCommentRespVO;
import cn.iocoder.yudao.module.blog.controller.app.space.vo.AppBlogSpaceRespVO;
import cn.iocoder.yudao.module.blog.convert.space.BlogSpaceConvert;
import cn.iocoder.yudao.module.blog.convert.spacecomment.BlogSpaceCommentConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.space.BlogSpaceDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.spacecomment.BlogSpaceCommentDO;
import cn.iocoder.yudao.module.blog.service.space.BlogSpaceService;
import cn.iocoder.yudao.module.blog.service.spacecomment.AppBlogSpaceCommentService;
import cn.iocoder.yudao.module.blog.service.spacecomment.BlogSpaceCommentService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "用户 APP - 评论")
@RestController
@RequestMapping("/blog/space/comment")
@Validated
public class AppBlogSpaceCommentController {


    @Resource
    private BlogSpaceService blogSpaceService;

    @Resource
    private BlogSpaceCommentService blogSpaceCommentService;

    @Resource
    private AppBlogSpaceCommentService appBlogSpaceCommentService;

    @Resource
    private MemberUserApi memberUserApi;


    @PostMapping("/create")
    @Operation(summary = "创建评论")
    public CommonResult<BlogSpaceCommentRespVO> createComment(@Valid @RequestBody BlogSpaceCommentCreateReqVO createReqVO) {
        Long id = blogSpaceCommentService.createSpaceComment(createReqVO);
        blogSpaceService.updateSpaceCommentNum(createReqVO.getSpaceId(), 1);

        BlogSpaceCommentDO blogSpaceCommentDO = blogSpaceCommentService.getSpaceComment(id);
        MemberUserRespDTO memberUserRespDTO = memberUserApi.getUser(blogSpaceCommentDO.getMemberId());
        return success(BlogSpaceCommentConvert.INSTANCE.convertApp(blogSpaceCommentService.getSpaceComment(id), memberUserRespDTO));
    }

    @GetMapping("/child")
    @Operation(summary = "获得动态评论")
    @Parameter(name = "commentId", description = "编号", required = true, example = "1024")
    public CommonResult<Long> childCommentCount(@RequestParam("commentId") Long commentId) {
        return success(blogSpaceCommentService.getChildCount(commentId));
    }

    @GetMapping("/page")
    @Operation(summary = "获得评论分页")
    public CommonResult<PageResult<AppBlogSpaceCommentRespVO>> getCommentPage(@Valid AppBlogSpaceCommentPageReqVO pageVO) {
        PageResult<BlogSpaceCommentDO> pageResult = appBlogSpaceCommentService.getSpaceCommentPage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), BlogSpaceCommentDO::getMemberId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        return success(BlogSpaceCommentConvert.INSTANCE.convertAppPage(pageResult, memberMap));
    }

    @GetMapping("/totalcount")
    @Operation(summary = "获得帖子总评论数量")
    @Parameter(name = "spaceId", description = "动态编号", required = true, example = "1024")
    public CommonResult<Long> getTotalCount(@RequestParam("spaceId") Long spaceId){
        return success(appBlogSpaceCommentService.getTotalCount(spaceId));
    }
}
